package controller

import (
	"github.com/gin-gonic/gin"
	"kf/models/dao"
	"kf/utils/R"
	"log"
	"net/http"
)

type ReplyForm struct {
	GroupName string `json:"group_name" form:"group_name" binding:"required"`
}
type ReplyContentForm struct {
	GroupId  string `json:"group_id" form:"group_id" binding:"required"`
	Content  string `json:"content" form:"content" binding:"required"`
	ItemName string `json:"item_name"  form:"item_name" binding:"required"`
}
type ReplyController struct {
	ReplyDao *dao.ReplyDao
}

func (replyController *ReplyController) GetReplys(c *gin.Context) {
	kefuId, _ := c.Get("kefu_name")
	log.Println(kefuId)
	res := replyController.ReplyDao.FindReplyByUserId(kefuId)
	c.JSON(http.StatusOK, R.ReturnSuccess(res))
}
func (replyController *ReplyController) GetAutoReplys(c *gin.Context) {
	kefu_id := c.Query("kefu_id")
	res := replyController.ReplyDao.FindReplyTitleByUserId(kefu_id)
	c.JSON(200, gin.H{
		"code":   200,
		"msg":    "ok",
		"result": res,
	})
}
func (replyController *ReplyController) PostReply(c *gin.Context) {
	var replyForm ReplyForm
	kefuId, _ := c.Get("kefu_name")
	err := c.ShouldBindJSON(&replyForm)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "error:" + err.Error(),
		})
		return
	}
	replyController.ReplyDao.CreateReplyGroup(replyForm.GroupName, kefuId.(string))
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}
func (replyController *ReplyController) PostReplyContent(c *gin.Context) {
	var replyContentForm ReplyContentForm
	kefuId, _ := c.Get("kefu_name")
	err := c.Bind(&replyContentForm)
	if err != nil {
		c.JSON(400, gin.H{
			"code": 200,
			"msg":  "error:" + err.Error(),
		})
		return
	}
	replyController.ReplyDao.CreateReplyContent(replyContentForm.GroupId, kefuId.(string), replyContentForm.Content, replyContentForm.ItemName)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}

type ReplyContentSaveForm struct {
	ReplyId      string `json:"reply_id" form:"reply_id" binding:"required"`
	ReplyContent string `json:"reply_content" form:"reply_content" binding:"required"`
	ReplyTitle   string `json:"reply_title"  form:"reply_title" binding:"required"`
}

func (replyController *ReplyController) PostReplyContentSave(c *gin.Context) {
	kefuId, _ := c.Get("kefu_name")
	var replyContentSaveForm ReplyContentSaveForm
	err := c.ShouldBindJSON(&replyContentSaveForm)
	if err != nil {
		c.JSON(400, gin.H{
			"code": 200,
			"msg":  "参数错误!",
		})
		return
	}
	replyController.ReplyDao.UpdateReplyContent(replyContentSaveForm.ReplyId,
		kefuId.(string),
		replyContentSaveForm.ReplyTitle,
		replyContentSaveForm.ReplyContent)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}
func (replyController *ReplyController) DelReplyContent(c *gin.Context) {
	kefuId, _ := c.Get("kefu_name")
	id := c.Query("id")
	replyController.ReplyDao.DeleteReplyContent(id, kefuId.(string))
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}
func (replyController *ReplyController) DelReplyGroup(c *gin.Context) {
	kefuId, _ := c.Get("kefu_name")
	id := c.Query("id")
	replyController.ReplyDao.DeleteReplyGroup(id, kefuId.(string))
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}
func (replyController *ReplyController) PostReplySearch(c *gin.Context) {
	kefuId, _ := c.Get("kefu_name")
	search := c.PostForm("search")
	if search == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	res := replyController.ReplyDao.FindReplyBySearch(kefuId, search)
	c.JSON(200, gin.H{
		"code":   200,
		"msg":    "ok",
		"result": res,
	})
}
